package com.flowable.project.rocketmq;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

//@Component
public class PayProducer {

    private String producerGroup = "flowable_produce_group";



//    private DefaultMQProducer producer;


//    public  PayProducer(){
//        producer = new DefaultMQProducer(producerGroup);
//
//        //生产者投递消息重复次数
//        producer.setRetryTimesWhenSendFailed(3);
//
//        //指定NameServer地址，多个地址以 ; 隔开
//        //如 producer.setNamesrvAddr("192.168.100.141:9876;192.168.100.142:9876;192.168.100.149:9876");
//
//        producer.setNamesrvAddr(JmsConfig.NAME_SERVER);
//        start();
//    }
//
//    public DefaultMQProducer getProducer(){
//        return this.producer;
//    }
//
//    /**
//     * 对象在使用之前必须要调用一次，只能初始化一次
//     */
//    public void start(){
//        try {
//            this.producer.start();
//        } catch (MQClientException e) {
//            e.printStackTrace();
//        }
//    }


    /**
     * 一般在应用上下文，使用上下文监听器，进行关闭
     */
//    public void shutdown(){
//        this.producer.shutdown();
//    }

    public void sendMessage(String msg,String tag,String key){
        DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
        producer.setRetryTimesWhenSendFailed(3);
        producer.setNamesrvAddr(JmsConfig.NAME_SERVER);
        try {
            producer.start();
            Message message = new Message(JmsConfig.TOPIC,tag,key,msg.getBytes());
            SendResult sendResult = producer.send(message);
            System.out.println("id:"+sendResult.getMsgId()+" result:"+sendResult.getSendStatus());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            producer.shutdown();
        }
    }
}
